﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;

namespace Com.OpenCode.Framework.DevForm
{
    public delegate void AddInEventHandle(MainForm form);

    public partial class MainForm : RibbonForm
    {
        private Timer timer=new Timer();

        #region 构造函数

        public MainForm(BuildItemEventArgs args)
        {
            Closed += MainForm_Closed;
            BuildItemEventArgs = args;
            InitializeComponent();
            args.Plugin.Owner = this;
            DockAreaControl = new XtraUserControl {Dock = DockStyle.Fill};
            Controls.Add(DockAreaControl);
            Instance = this;
            WindowState = FormWindowState.Maximized;
            Text = args.Plugin.Properties.Get("Title").ToString();
            string _IconName = args.Plugin.Properties.Get("Image").ToString();
            string _IconPath = "";
            if (File.Exists(_IconPath))
            {
                Icon = new Icon(_IconPath);
            }
            DockManagerUI = new DockManagerUI(this);
        }

        private void MainForm_Closed(object sender, EventArgs e)
        {
            BuildItemEventArgs.Plugin.ClearOwner();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (StartLoad != null)
            {
                timer.Interval = 500;
                timer.Tick += timer_Tick;
                timer.Start();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            StartLoad(this);
        }

        #endregion

        public event AddInEventHandle StartLoad;

        public BuildItemEventArgs BuildItemEventArgs { get;private set; }

        public static MainForm Instance { get; private set; }

        public XtraUserControl DockAreaControl { get; private set; }

        public DockManagerUI DockManagerUI { get; private set; }
    }
}